//Papr. 1.0
// Copyright (c) 2012 Subhodip Kumar. All rights reserved.
 



var self=this;
function dumpBookmarks(query) {
  var bookmarkTreeNodes = chrome.bookmarks.getTree(
    function(bookmarkTreeNodes) {
    console.log(bookmarkTreeNodes);
    //stopping case
    var count;
    var bk=bookmarkTreeNodes;
    var parentId=null;
    for (count=0; count<bk.length ; count++){
    	if(bk[count].title){ 
    		
    	}
    }
    
      $('#db-results').append(dumpTreeNodes(bookmarkTreeNodes, query));
    });
}
function dumpTreeNodes(bookmarkNodes, query) {
  var list = $('<span id="parent">');
  var i;
  for (i = 0; i < bookmarkNodes.length; i++) {
    list.append(dumpNode(bookmarkNodes[i], query));
  }
  
  
  return list;
}
////test

  
function dumpNode(bookmarkNode, query) {
  if (bookmarkNode.title) {
    if (query && !bookmarkNode.children) {
      if (String(bookmarkNode.title).indexOf(query) == -1) {
        return $('<span></span>');
      }
    }
    var anchor = $('<a>');
    anchor.attr('href', bookmarkNode.url);//bookmarkNode.url
    anchor.text(bookmarkNode.title);
    /////test
   var img_1=$('<img src="bullets_blue.png" style="height: 17px; max-width: 100%;  padding-right:9px; float:left;" />');
    var img_2=$('<img src="bullets_orange.png" style="float:left; height: 14px;max-width: 100%;padding-left:1px;padding-right:9px;" />');
    var a_cont=$('<div style="padding-left:25px;padding-bottom:10px;">');
    shrturl=$('<span id="shrtUrl">');
    if(bookmarkNode.url){
    shrturl.text(shortenUrl(bookmarkNode.url));
    }else {shrturl.text("")}
   
    anchor.click(function() {
      chrome.tabs.create({url: bookmarkNode.url});
    });
    var div = $(bookmarkNode.title && bookmarkNode.url ?'<div id="cont">':'div id="folder"');
    var options = $('<div id="del"class="hover"><i class="icon-remove-sign"></i></div>');
    // Show add and edit links when hover over.
        div.hover(function() {
        div.append(options);
        $('#del').click(function() {
                   
                   //console.log("out"+out);
                   chrome.bookmarks.remove(String(bookmarkNode.id));
                	div.parent().remove();
                   chrome.extension.getBackgroundPage().user_notify("Bookmark deleted","");
         });
      },
      // unhover
      function() {
        options.remove();
      }).append(img_1);
      a_cont.append(anchor);
      div.append(a_cont);
      div.append(img_2);
      div.append(shrturl);
      
  }
  var li = $(bookmarkNode.title ? '<span id="member">' : '<span id="blank">').append(div);//
  if (bookmarkNode.children && bookmarkNode.children.length > 0) {
    li.append(dumpTreeNodes(bookmarkNode.children, query));
  }
  return li;
}
var shortenUrl=function(url) {
        	var start=url.search("//");
			start=start+2;
			
			url=url.substring(start);
			
			end=url.search("/");
			short_url=url.substring(0,end);
			return short_url;
        	};
